home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / SequenceableCollection.st < prev    next >
Text File  |  2000-02-13  |  4KB  |  154 lines

  1. Class SequenceableCollection :KeyedCollection
  2. [
  3.    , aCollection
  4.       ^ self coerce: (List new ; 
  5.          addAllLast: self ;
  6.          addAllLast: aCollection )
  7. |
  8.    collect: aBlock 
  9.       ^ self coerce:
  10.          (self inject: List new
  11.                  into: [:x :y | x addLast: (aBlock value: y) . x ] )
  12. |
  13.    copyFrom: start to: stop  ! newcol !
  14.       newcol <- List new.
  15.  
  16.       (start to: stop) do: [:i | newcol addLast: (self at: i)].
  17.  
  18.       ^ self coerce: newcol
  19. |
  20.    copyWith: newElement
  21.       ^ self coerce: (List new ; 
  22.          addAllLast: self ;
  23.             addLast: newElement )
  24. |
  25.    copyWithout: oldElement ! newcol !
  26.       newcol <- List new.
  27.  
  28.       self do: [ :x | (x == oldElement) 
  29.                       ifFalse: [ newcol addLast: x ] ].
  30.       
  31.       ^ self coerce: newcol
  32. |
  33.    equals: aSubCollection startingAt: anIndex  ! i !
  34.       i <- 0.
  35.  
  36.       self do: [:x |
  37.                 (x = (aSubCollection at: (anIndex + i)
  38.                                ifAbsent: [^ false]))
  39.                      ifFalse: [^ false].
  40.                 i <- i + 1].
  41.       ^ true
  42. |
  43.    findFirst: aBlock
  44.       ^ self findFirst: aBlock 
  45.               ifAbsent: [self error: 'first element not found']
  46. |
  47.    findFirst: aBlock ifAbsent: exceptionBlock
  48.       self do: [:x | (aBlock value: x) 
  49.                      ifTrue: [ ^ self currentKey] ].
  50.  
  51.       ^ exceptionBlock value
  52. |
  53.    findLast: aBlock
  54.       self findLast: aBlock
  55.            ifAbsent: [self error: 'last element not found']
  56. |
  57.    findLast: aBlock ifAbsent: exceptionBlock
  58.       self reverseDo: [:x | (aBlock value: x) 
  59.                             ifTrue: [ ^ self currentKey] ].
  60.  
  61.       ^ exceptionBlock value
  62. |
  63.    indexOfSubCollection: aSubColl startingAt: anIndex ifAbsent: exceptBlock
  64.       ! n m !
  65.       n <- anIndex.
  66.       m <- self size - aSubColl size.
  67.  
  68.       [n <= m] whileTrue:
  69.          [(aSubColl equals: self startingAt: n)
  70.  
  71.                     ifTrue: [^ n].
  72.  
  73.                 n <- n + 1].
  74.  
  75.       ^ exceptBlock value
  76. |
  77.    indexOfSubCollection: aSubCollection startingAt: anIndex
  78.       ^ self indexOfSubCollection: aSubCollection 
  79.                        startingAt: anIndex
  80.                          ifAbsent: [ self error: 'element not found'. nil]
  81. |
  82.    last
  83.       ^ (0 = self size) ifFalse: [ self at: self lastKey ]
  84. |
  85.    replaceFrom: start to: stop with: repcol
  86.       repcol inject: start
  87.                into: [:x :y | self at: x put: y. x + 1]
  88. |
  89.    replaceFrom: first to: stop with: repcol startingAt: repStart ! i !
  90.       i <- 0 .
  91.  
  92.       [(first + i) <= stop] whileTrue:
  93.                              [self at: (first + i)
  94.                                   put: (repcol at: i + repStart).
  95.                                        i <- i + 1 ]
  96. |
  97.    reverseDo: aBlock    ! n m !
  98.       n <- self lastKey.   
  99.       m <- self firstKey.
  100.       
  101.       [n >= m] whileTrue:
  102.                [(self includesKey: n) ifTrue: [aBlock value: (self at: n)].
  103.                   n <- n - 1].
  104.  
  105.       ^ nil
  106. |
  107.    reversed ! newar i !
  108.       newar <- Array new: (i <- self size).
  109.  
  110.       self do: [:x | newar at: i put: x. i <- i - 1].
  111.  
  112.       ^ self coerce: newar
  113. |
  114.    select: aBlock      
  115.       ^ self coerce:
  116.          (self inject: List new
  117.                  into: [:x :y | (aBlock value: y)
  118.                                 ifTrue: [x addLast: y]. x ] )
  119. |
  120.    sort
  121.       ^ self sort: [:x :y | x <= y]
  122. |
  123.    sort: sortBlock ! index temp newArray !
  124.       newArray <- self asArray.
  125.  
  126.       (2 to: newArray size) do:
  127.          [ :highIndex | index <- highIndex - 1.
  128.             [(index >= 1) and:
  129.                [(sortBlock value: (newArray at: index)
  130.                            value: (newArray at: (index + 1))) not]]
  131.             
  132.                whileTrue: [temp <- newArray at: index.
  133.                                    newArray at: index 
  134.                                            put: (newArray at: index + 1).
  135.               
  136.                                    newArray at: index + 1 put: temp.
  137.                            index <- index - 1 ]].
  138.       
  139.       ^ self coerce: newArray
  140.  
  141. |
  142.    with: aSequenceableCollection do: aBlock 
  143.      ! arg1 arg2 !
  144.  
  145.       arg1 <- self first. arg2 <- aSequenceableCollection first.
  146.  
  147.       [ arg1 notNil] whileTrue:
  148.                       [ aBlock value: arg1 value: arg2.
  149.                           arg1 <- self next.
  150.                           arg2 <- aSequenceableCollection next].
  151.  
  152.       ^ nil
  153. ]
  154.